﻿using System;

public class Memory
{
    public short[] mem;
    internal int memLength;
    internal NES nes;

    public Memory(NES nes, int byteCount)
    {
        this.nes = nes;
        this.mem = new short[byteCount];
        this.memLength = byteCount;
    }

    public virtual void destroy()
    {
        this.nes = null;
        this.mem = null;
    }

    public virtual void dump(string file)
    {
        this.dump(file, 0, this.mem.Length);
    }

    public virtual void dump(string file, int offset, int length)
    {
    }

    public virtual short load(int address)
    {
        return this.mem[address];
    }

    public virtual void reset()
    {
        for (int i = 0; i < this.mem.Length; i++)
        {
            this.mem[i] = 0;
        }
    }

    public virtual void stateLoad(ByteBuffer buf)
    {
        if (this.mem == null)
        {
            this.mem = new short[this.memLength];
        }
        buf.readByteArray(this.mem);
    }

    public virtual void stateSave(ByteBuffer buf)
    {
        buf.putByteArray(this.mem);
    }

    public virtual void write(int address, short value_Renamed)
    {
        this.mem[address] = value_Renamed;
    }

    public virtual void write(int address, short[] array, int length)
    {
        if ((address + length) <= this.mem.Length)
        {
            Array.Copy(array, 0, this.mem, address, length);
        }
    }

    public virtual void write(int address, short[] array, int arrayoffset, int length)
    {
        if ((address + length) <= this.mem.Length)
        {
            Array.Copy(array, arrayoffset, this.mem, address, length);
        }
    }

    public virtual int MemSize
    {
        get
        {
            return this.memLength;
        }
    }
}

